package com.dianrui.operation.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dianrui.operation.R;
import com.dianrui.operation.bean.Repair;
import com.dianrui.operation.util.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RepairNewOrderListAdapter extends BaseAdapter {
    Context context;
    BatteryClickListener batteryClickListener;
    List<Repair> repairList;
//    int tag;//0 未入库, 1 需入库, 2已入库


    public RepairNewOrderListAdapter(Context context, List<Repair> repairList) {
        this.context = context;
        this.repairList=repairList;
    }
    public void refresh(List<Repair> list) {
        this.repairList = list;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (repairList == null) {
            return 0;
        }
        return repairList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface BatteryClickListener {
        void onBatteryClick(String degree,String maintain_id, String form_name,String lat, String lng, String status, String title, String number);

    }

    public void setOnBatteryClickListener(BatteryClickListener messageClickListener) {
        this.batteryClickListener = messageClickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_repairorder_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (repairList.size()>0) {
            final Repair repair = repairList.get(position);
            holder.carNumbers.setText("工单号: "+repair.order_code);
            holder.number.setText("车辆编号："+repair.number);
            if (StringUtils.isEmpty(repair.form_name)) {
                holder.pjoectname.setText("暂无");
            }else {
                holder.pjoectname.setText(repair.form_name);
            }
            holder.create_time.setText("创建时间："+repair.create_time);
            if (repair.state.equals("0")) {
                holder.battery_status.setText("待处理 >");
                holder.battery_status.setTextColor(Color.RED);
            } else if  (repair.state.equals("1")) {
                holder.battery_status.setText("已结束 >");
                holder.battery_status.setTextColor(Color.RED);
            }


            holder.click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    batteryClickListener.onBatteryClick(repair.degree,repair.maintain_id,repair.form_name,repair.glat,repair.glng,repair.state,repair.contents,repair.number);
                }
            });

        }

        return convertView;
    }


     class ViewHolder {
        @BindView(R.id.numbers)
        TextView carNumbers;
        @BindView(R.id.pjoectname)
        TextView pjoectname;
         @BindView(R.id.battery_status)
         TextView battery_status;

         @BindView(R.id.number)
         TextView number;
         @BindView(R.id.create_time)
         TextView create_time;
         @BindView(R.id.repair_item_layout)
         RelativeLayout click;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
